//
//  UIAlertView+MKBlockAdditions.m
//  UIKitCategoryAdditions
//
//  Created by Mugunth on 21/03/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "UIAlertView+Block.h"
#import <objc/runtime.h>

@implementation UIAlertView (Block)

static id kUIAlertView_BBExtensionsCompletionBlockKey;


#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView*)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    void (^completion)(UIAlertView* alertView, NSInteger buttonIndex);
    completion = objc_getAssociatedObject(self, &kUIAlertView_BBExtensionsCompletionBlockKey);
    
    if (completion != nil) completion(alertView, buttonIndex);
}


#pragma mark Class interface

+ (void)showAlertWithTitle:(NSString*)title
{
    [[[[self class] alloc]
      initWithTitle:title message:nil delegate:nil
      cancelButtonTitle:@"确认" otherButtonTitles:nil]
     show];
}

+ (void)showAlertWithTitle:(NSString*)title andMessage:(NSString*)message
{
    [[[[self class] alloc]
      initWithTitle:title message:message delegate:nil
      cancelButtonTitle:@"确认" otherButtonTitles:nil]
     show];
}

+ (instancetype)noticeWithTitle:(NSString*)title message:(NSString*)message buttonTitle:(NSString*)buttonTitle
                     completion:(void (^)())completion
{
    UIAlertView* alertView = [[[self class] alloc]
                              initWithTitle:title message:message delegate:nil
                              cancelButtonTitle:buttonTitle otherButtonTitles:nil];
    alertView.delegate = alertView;
    [alertView setCompletion:^(UIAlertView* alertView, NSInteger buttonIndex) {
        completion();
    }];
    
    return alertView;
}

+ (instancetype)inputWithTitle:(NSString*)title submission:(void (^)(NSString* text))submission
{
    UIAlertView* alertView = [[[self class] alloc]
                              initWithTitle:title message:nil delegate:nil
                              cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    __weak id weakRef = alertView;
    [alertView setCompletion:^(UIAlertView* alertView, NSInteger buttonIndex) {
        if (buttonIndex == 1) submission([weakRef textFieldAtIndex:0].text);
    }];
    
    return alertView;
}

+ (instancetype)confirmationWithTitle:(NSString*)title confirmation:(void (^)())confirmation
{
    UIAlertView* alertView = [[[self class] alloc]
                              initWithTitle:title message:nil delegate:nil
                              cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    
    [alertView setCompletion:^(UIAlertView* alertView, NSInteger buttonIndex) {
        if (buttonIndex == 1) confirmation();
    }];
    
    return alertView;
}

+ (instancetype)informationWithTitle:(NSString*)title confirmation:(void (^)())confirmation
{
    UIAlertView* alertView = [[[self class] alloc]
                              initWithTitle:title message:nil delegate:nil
                              cancelButtonTitle:@"确认" otherButtonTitles:nil];
    
    [alertView setCompletion:^(UIAlertView* alertView, NSInteger buttonIndex) {
        if (confirmation) {
            confirmation();
        }
    }];
    
    return alertView;
}

- (instancetype)initWithTitle:(NSString*)title message:(NSString*)message
            cancelButtonTitle:(NSString*)cancelButtonTitle otherButtonTitles:(NSArray*)otherButtonTitles
                   completion:(void (^)(UIAlertView* alertView, NSInteger buttonIndex))completion
{
    self = [super init];
    if (self != nil) {
        self.title = title;
        self.message = message;
        
        for (NSString* buttonTitle in otherButtonTitles) {
            [self addButtonWithTitle:buttonTitle];
        }
        [self addButtonWithTitle:cancelButtonTitle];
        [self setCancelButtonIndex:([self numberOfButtons] - 1)];
        
        self.delegate = self;
        [self setCompletion:completion];
    }
    
    return self;
}


#pragma mark Interface

- (void)setCompletion:(void (^)(UIAlertView* alertView, NSInteger buttonIndex))completion
{
    if (completion == nil) return;
    
    self.delegate = self;
    objc_setAssociatedObject(self, &kUIAlertView_BBExtensionsCompletionBlockKey,
                             completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

@end